#!/bin/sh

#username=ubnt
#password=ubnt
#baseurl=https://unifi:8443
cookie=/tmp/unifi_cookie

curl_cmd="curl --sslv3 --silent --cookie ${cookie} --cookie-jar ${cookie} --insecure "


named_args_to_payload() {
    payload=""
    for a in "$@" ; do
        if [ "${a##*=*}" = "" ] ; then
            k=`echo $a | cut -d = -f 1`
            v=`echo $a | cut -d = -f 2`
            payload="${payload}, '$k':'$v'"
        fi
    done
    echo ${payload}
}

unifi_requires() {
    if [ -z "$username" -o -z "$password" -o -z "$baseurl" ] ; then
        echo "Error! please define required env vars before including unifi_sh. E.g. "
        echo ""
        echo "username=ubnt"
        echo "password=ubnt"
        echo "baseurl=http://unifi:8443"
        echo ""
        exit -1
    fi
}

unifi_login() {
    # authenticate against unifi controller
    ${curl_cmd} --data "login=login" --data "username=$username" --data "password=$password" $baseurl/login
}

unifi_logout() {
    # logout
    ${curl_cmd} $baseurl/logout
}

# cmd/stamgr
# authorize-guest(mac, minutes, [up=kbps, down=kbps, bytes=MB])
unifi_authorize_guest() {
    if [ $# -lt 2 ] ; then
        echo "Usage: $0 <mac> <minutes> [up=kbps] [down=kbps] [bytes=MB]"
        exit -1
    fi

    mac=$1
    minutes=$2
    other_payload=`named_args_to_payload "$@"`

    ${curl_cmd} --data "json={'cmd':'authorize-guest', 'mac':'${mac}', 'minutes':${minutes}${other_payload}}" $baseurl/api/cmd/stamgr
}

# cmd/stamgr
# unauthorize-guest(mac)
unifi_unauthorize_guest() {
    if [ $# -lt 2 ] ; then
        echo "Usage: $0 <mac>"
        exit -1
    fi

    mac=$1

    ${curl_cmd} --data "json={'cmd':'unauthorize-guest', 'mac':'${mac}'}" $baseurl/api/cmd/stamgr
}

# cmd/stamgr
# kick-sta(mac)
unifi_reconnect_sta() {
    if [ $# -lt 1 ] ; then
        echo "Usage: $0 <mac>"
        exit -1
    fi

    mac=$1

    ${curl_cmd} --data "json={'cmd':'kick-sta', 'mac':'${mac}'}" $baseurl/api/cmd/stamgr
}

# cmd/stamgr
# block-sta(mac)
unifi_block_sta() {
    if [ $# -lt 1 ] ; then
        echo "Usage: $0 <mac>"
        exit -1
    fi

    mac=$1

    ${curl_cmd} --data "json={'cmd':'block-sta', 'mac':'${mac}'}" $baseurl/api/cmd/stamgr
}

# cmd/stamgr
# update-sta(mac, name, email)
unifi_update_sta() {
    if [ $# -lt 1 ] ; then
        echo "Usage: $0 <mac> <name> <email>"
        exit -1
    fi

    mac=$1

    ${curl_cmd} --data "json={'cmd':'update-sta', 'mac':'${mac}', 'name':'$2', 'email':'$3'}" $baseurl/api/cmd/stamgr
}

unifi_backup() {
    if [ "$1" = "" ]; then
        output=unifi-backup.unf # or `date +%Y%m%d`.unf
    else
        output=$1
    fi

    # ask controller to do a backup, response contains the path to the backup file
    path=`$curl_cmd --data "json={'cmd':'backup'}" $baseurl/api/cmd/system | sed -n 's/.*\(\/dl.*unf\).*/\1/p'`

    # download the backup to the destinated output file
    $curl_cmd $baseurl$path -o $output
}

# cmd/hotspot
# create-voucher(expires, n, [note=notes, up=kbps, down=kbps, bytes=MB])
# @returns create_time
unifi_create_voucher() {
    if [ $# -lt 2 ] ; then
        echo "Usage: $0 <minutes> <n> [note=notes] [up=kbps] [down=kbps] [bytes=MB]"
        exit -1
    fi
    minutes=$1
    n=$2
    other_payload=`named_args_to_payload "$@"`
    token=`${curl_cmd} --data "json={'cmd':'create-voucher','expire':${minutes},'n':$n ${other_payload}}" $baseurl/api/cmd/hotspot \
        | sed -e 's/.*"create_time"\s*:\s*\([0-9]\+\).*/\1/'`
    echo "token=$token"
    if [ "$token" != "" ] ; then
        ${curl_cmd} --data "json={'create_time':${token}}" $baseurl/api/stat/voucher
    fi
}

# stat/voucher
# query(create_time)
unifi_get_vouchers() {
    if [ $# -lt 1 ] ; then
        echo "Usage: $0 <token>"
        exit -1
    fi
    token=$1
    ${curl_cmd} --data "json={'create_time':${token}}" $baseurl/api/stat/voucher
}

# stat/sta
unifi_list_sta() {
    ${curl_cmd} --data "json={}" $baseurl/api/stat/sta
}


unifi_requires

